﻿using System;
using System.Drawing;
using System.Collections.Generic;
using System.Text;
using DirectUI.Mobile.UI.Controls;
using RecorderSample.Controller;
using System.Windows.Forms;
using System.IO;
namespace RecorderSample.View
{
  public   class AudioRecordPage:Page
    {
      private AudioRecordControler _controler;
      private DirectUILabel _lbTime;
      private AnimateProgressBar _pbar;
      private DirectUIButton _btClose;
      private DirectUIButton _btRedo;
      private Timer _timer;
      protected override void OnSizeChanged(Size oldSize, Size newSize)
      {
          base.OnSizeChanged(oldSize, newSize);
          if (statusPanl != null)
          {
              statusPanl.Top = this.Height - 93;
              statusPanl.Left = 0;
              statusPanl.Width = this.Width;
              statusPanl.Height = 93;
          }
      }
      private DirectUIPanel statusPanl;
      public AudioRecordPage()
          : base()
      {
          this.Name = "Record";
          this.BackGroundImage = Resource.Audiobg2;
          _controler = new AudioRecordControler(this);
         statusPanl = new DirectUIPanel();
          //statusPanl.BackColor = Color.FromArgb(6, 139, 204);
          //statusPanl.EnableDoubleBuffer = true;
          //statusPanl.Alpha = 200;
          //statusPanl.GradientFill = true;
          //statusPanl.GradientFillOffset = 20;
          statusPanl.BackGroundImage = Resource.RecordBg;
        
         
          _lbTime = new DirectUILabel();
          _lbTime.Width = 100;
          _lbTime.Left = 10;
          _lbTime.Top = 15;
          _lbTime.Height = 20;
          _lbTime.ForeColor = Color.White;
          _lbTime.Text = "00:00";
          _pbar = new AnimateProgressBar();
          _pbar.Left = 10;
          _pbar.Top = 40;

          _btClose = new DirectUIButton();
          _btClose.Shape = ButtonShape.Flat;
          _btClose.Bounds = new Rectangle(190, 60, 50, 30);
          _btClose.Image = Resource.AudioClose;
          _btClose.Click += new EventHandler(_btClose_Click);
          //_btClose.Command = "Delete";
          statusPanl.Controls.Add(_btClose);
          _btRedo = new DirectUIButton();
          _btRedo.Shape = ButtonShape.Flat;
          _btRedo.Bounds = new Rectangle(140, 60, 50, 30);
          _btRedo.Image = Resource.AudioReDo;
          _btRedo.Click += new EventHandler(_btRedo_Click);
          statusPanl.Controls.Add(_btRedo);
          statusPanl.Controls.Add(_lbTime);
          statusPanl.Controls.Add(_pbar);
          this.Controls.Add(statusPanl);
          _timer = new Timer();
          _timer.Interval = 300;
          _timer.Enabled = false;
          _timer.Tick += new EventHandler(_timer_Tick);
      }

      void _btClose_Click(object sender, EventArgs e)
      {
          try
          {
              Stop();

              _controler.Close();
          }
          catch (Exception ex)
          {
              MessageDialog.Show(ex.Message, "", null);
          }

      }

      void _btRedo_Click(object sender, EventArgs e)
      {
          try
          {
              _pbar.Value = 0;
              _startTime = DateTime.Now;
              _controler.Stop();
              String filePath = System.Reflection.Assembly.GetExecutingAssembly().GetName().CodeBase;
              filePath = Path.GetDirectoryName(filePath);
              filePath = Path.Combine(filePath, Guid.NewGuid().ToString()+".wav");
              _filePath = filePath;
              _controler.Start(_filePath);
          }
          catch(Exception ex)
          {
              MessageDialog.Show(ex.Message, "", null);
          }
      }
      private DateTime _startTime = DateTime.Now;
      public int TotalMinute = 2;
      void _timer_Tick(object sender, EventArgs e)
      {
          TimeSpan sp = DateTime.Now - _startTime;
          _lbTime.Text = sp.Minutes.ToString() + " : " + sp.Seconds.ToString();
          _pbar.Value += 0.5f;
          if (sp.Minutes == 2)
          {
              Stop();
          }
          
      }
     
      public void Start()
      {
          try
          {
              _lbTime.Text = "00:00";
              _startTime = DateTime.Now;
              _pbar.Value = 0;
              _timer.Enabled = true;
              String filePath = System.Reflection.Assembly.GetExecutingAssembly().GetName().CodeBase;
              filePath = Path.GetDirectoryName(filePath);
              if(!Directory.Exists(Path.Combine(filePath, "SoundTemp\\"))){
                  Directory.CreateDirectory(Path.Combine(filePath, "SoundTemp\\"));
              }
              filePath = Path.Combine(filePath, "SoundTemp\\"+Guid.NewGuid().ToString()+".wav");
              _filePath = filePath;
              _controler.Start(_filePath);
          }
          catch (Exception ex)
          {
              MessageDialog.Show(ex.Message, "", null);
          }
      }
      private string _filePath;
      public void Stop()
      {
          if (_timer.Enabled)
          {
              _timer.Enabled = false;
              _controler.Stop();
          }
      }
      public override void Dispose()
      {
          base.Dispose();
          _controler.Dispose();
      }
    }
}
